package com.qiezi.project.dialog;

import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hjq.base.BaseDialog;
import com.hjq.base.BaseDialogFragment;
import com.qiezi.R;

import java.text.NumberFormat;
import java.text.ParseException;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/12/2
 *    desc   : 消息对话框
 */
public class GetMoneyDialog {

    public static class Builder
            extends BaseDialogFragment.Builder<Builder>
            implements View.OnClickListener {

        private OnListener mListener;





        public Builder(FragmentActivity activity) {
            super(activity);
            initialize();
        }

        public Builder(FragmentActivity activity, int themeResId) {
            super(activity, themeResId);
            initialize();
        }

        private EditText etMoney;
        private TextView tvAll,tvGetMoney,tvClose;

        private void initialize() {
            setContentView(R.layout.dialog_get_money);
            setAnimStyle(BaseDialog.AnimStyle.SCALE);
            setGravity(Gravity.CENTER);

            etMoney = findViewById(R.id.et_money);
            tvAll = findViewById(R.id.tv_all);
            tvGetMoney = findViewById(R.id.tv_getMoney);
            tvClose = findViewById(R.id.tv_close);

            tvAll.setOnClickListener(this);
            tvGetMoney.setOnClickListener(this);
            tvClose.setOnClickListener(this);

            etMoney.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.toString().length() > 0){
                        tvGetMoney.setEnabled(true);
                    }else {
                        tvGetMoney.setEnabled(false);
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }

        private float money = 0.f;

        public Builder setHintMoney(String money){
            NumberFormat format = NumberFormat.getInstance();
            try {
                this.money = format.parse(money).floatValue();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (this.money == 0){
                tvAll.setVisibility(View.GONE);
                etMoney.setEnabled(false);
                etMoney.setHint("当前无可提现金额");
                return this;
            }
            etMoney.setHint("可提现金额"+money + "元");
            return this;
        }


        public Builder setMoney(String money){
            etMoney.setText(money);
            return this;
        }


        public Builder setListener(OnListener l) {
            mListener = l;
            return this;
        }


        /**
         * {@link View.OnClickListener}
         */
        @Override
        public void onClick(View v) {
            if (mListener == null) return;

            if (v == tvAll) {
                mListener.all(getDialog(),this);
            }else if (v == tvGetMoney) {
                mListener.confirm(getDialog(),etMoney.getText().toString());
            }else if(v == tvClose){
                mListener.cancel(getDialog());
                dismiss();
            }
        }


    }


    public interface OnListener {

        /**
         * 点击全部时回调
         */
        void all(Dialog dialog,GetMoneyDialog.Builder builder);

        /**
         * 点击确定时回调
         */
        void confirm(Dialog dialog, String money);
        /**
         * 点击取消时回调
         */
        void cancel(Dialog dialog);
    }
}